#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 3e4 + 10;

int n,m,b[N],p[N],st,ed;
bitset < N > vis[N];
vector < int > hav[N];

struct node { int x,y,dis; };
bool operator  < (node a,node b) { return a.dis > b.dis; }
queue < node > q;

void insert(node u) {
    for(auto y : hav[u.x]) {
	if(vis[u.x].test(y) == 0) { vis[u.x].set(y); q.push((node) { u.x,y,u.dis }); }
    } vec().swap(hav[u.x]);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    n = in,m = in;
    for(int i = 1;i <= m;i++) { b[i] = in + 1,p[i] = in; hav[b[i]].pb(p[i]); }
    st = b[1]; ed = b[2]; if(st == ed) return puts("0"),0;
    node cur = (node) { st,hav[st][0],0 }; insert(cur);
    while(q.size()) {
	node u = q.front(),v; q.pop();
	v = u; v.x = u.x - u.y; v.dis++;
	if(v.x == ed) { printf("%d\n",v.dis); return 0; }
	if(v.x > 0 && v.x <= n && !vis[v.x].test(v.y)) vis[v.x].set(v.y),q.push(v),insert(v);
	v.x = u.x + u.y;
	if(v.x == ed) { printf("%d\n",v.dis); return 0; }
	if(v.x > 0 && v.x <= n && !vis[v.x].test(v.y)) vis[v.x].set(v.y),q.push(v),insert(v);
    }
    puts("-1"); return 0;
}
